<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者:奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
class Validate extends Base
{
   public function initialize()
    {
       parent::initialize();
       $this->isLogin();
    }
//邮箱绑定
public function index()
{
  $param = $this->request->param();


  return $this->fetch();
}
//邮箱绑定
public function email()
{
  $param = $this->request->param();

  $token = !empty($param['token'])?stripslashes(htmlspecialchars_decode($param['token'])):'';

  if(!empty($token)){
        $str = encrypt_decrypt($token,true); //解密
        $arr = explode('|',$str);
      
        if(empty($arr)){
          $this->error('邮箱验证失败','@user/validate/email');
       }
        //判断是有超过5分钟
        if(time() - $arr[2] > 300){
          $this->error('邮箱验证超时','@user/validate/email');
      }
        $where['username'] = $arr[0];
        $where['email'] = $arr[1];
        $res = model('user')->where($where)->find();
        if(empty($res)){
           $this->error('邮箱验证失败','@user/validate/email');
        }
        if($res['is_email'] ==1){
          $this->error('已经验证过了，无需重复验证','@user/index/index');
        }

        $data2['is_email'] =1;
        $where2['username'] = $arr[0];
        model('user')->editData($data2,$where2);
       $this->success('邮箱已验证成功','@user/index/index');      
  }
  return $this->fetch();
}
  public function sendEmail()
  {
  	 $param =$this->request->param();

       $rule =[    
          'email'=>'require|email',
        ];
        $msg= [
         'email.require' =>'邮箱不存在',
         'email.email' =>'邮箱格式错误',
  
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  //调用common 公共控制器中的 checkSubmit 方法
       if($error){
              return error_json($error);
         }
          if(cookie('ql_email')){

            return error_json('请稍后60秒后重新发送','',-1);
          }
     $email = trim($param['email']); //邮箱
      cookie('ql_email',$email,60); 
       $token =  session('username').'|'.$email.'|'.time();
      $url = $this->request->root(true).'/user/validate/email?token='.urlencode(encrypt_decrypt($token));
        $subject = $this->settings['site']['sitename'].'激活邮件';
        $body = '<table border="0" cellpadding="0" cellspacing="0" style="width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555;; font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;"> <tbody style="margin: 0; padding: 0;"> <tr style="background-color: #393D49; height: 60px; margin: 0; padding: 0;"> <td style="margin: 0; padding: 0;"> <div style="color: #5EB576; margin: 0; margin-left: 30px; padding: 0;"><a style="font-size: 14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;" href="http://fly.layui.com/" target="_blank" rel="noopener">'.$this->settings['site']['sitename'].'</a></div> </td> </tr> <tr style="margin: 0; padding: 0;"> <td style="margin: 0; padding: 30px;"> <p style="line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;"> Hi，<em style="font-weight: 700;">'.session('username').'</em>，请完成以下操作： </p> <div style=""> <a href="'.$url.'" style="background-color: #009E94; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;" target="_blank" rel="noopener">立即激活邮箱</a> </div> <p style="line-height: 20px; margin-top: 20px; padding: 10px; background-color: #f2f2f2; font-size: 12px;"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td> </tr> <tr style="background-color: #fafafa; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;"> <td style="margin: 0; padding: 0;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table>';
        $result =  send_email($email,$subject,$body);
      if($result === true){
        return success_json('验证邮件已经发送成功');
      }else{
          return error_json($result);
      }
  	
  }
}